###############
# Figure 8A
###############

data<-read.csv("figure8a.csv",header=T,as.is=T)

data$congress <- as.factor(data$congress)
data$speaker_bioguide <- as.factor(data$speaker_bioguide)
data$after <- as.factor(data$after)


modela <- lm("mean~after+congress + speaker_bioguide", data=data, weights=n)
summary(modela)
confint(modela)
Effa <- Effect(c("after"), modela)

predsa <- as.data.frame(Effa)

scaleFUN <- function(x) sprintf("%.2f", x)
pa <- ggplot(data=predsa, aes(y=fit, x=after, ymin=lower, ymax=upper)) +
	geom_point() +
	geom_errorbar(width = .01) +
	xlab("Period +/- 60 days\naround primaries") +
	ylab("Percent of speeches engaing bipartisanship")+
	ggtitle("(A) Bipartisan Rhetoric")+ 
	coord_flip()+
	theme_pew()+ 
	scale_colour_manual(values=c("blue", "red")) + 
	scale_fill_manual(values=c("blue", "red")) +
	theme(legend.position = "none") +
	scale_y_continuous(breaks=seq(2,3,by=.2), limits = c(2,3),labels=scaleFUN)

pa



###############
# Figure 8B
###############

summary_data <- read_csv("figure8b.csv")

summary_data$congress <- as.factor(summary_data$congress)
summary_data$bioguide_id <- as.factor(summary_data$bioguide_id)

summary_data$mean <-  (1- summary_data$mean)*100

modelb <- lm("mean~after+ congress + bioguide_id", data=summary_data)
summary(modelb)
confint(modelb)
Effb <- Effect(c("after"), modelb)

predsb <- as.data.frame(Effb)

scaleFUN <- function(x) sprintf("%.2f", x)
pb <- ggplot(data=predsb, aes(y=fit, x=after, ymin=lower, ymax=upper)) +
	geom_point() +
	geom_pointrange(aes(ymin=lower, ymax=upper)) +
	xlab("") +
	ylab("Percentage of defections")+
	ggtitle("(B) Bipartisan Voting")+ 
	coord_flip()+
	theme_pew()+ 
	scale_colour_manual(values=c("blue", "red")) + 
	scale_fill_manual(values=c("blue", "red")) +
	theme(legend.position = "none") +
	scale_y_continuous(breaks=seq(20,30,by=2), limits = c(20,30),labels=scaleFUN)

pb

###############
# Figure 8C
###############

summary_data <- read_csv("figure8c.csv")

summary_data$congress <- as.factor(summary_data$congress)
summary_data$bioguide_id <- as.factor(summary_data$bioguide_id)
summary_data$after <- as.factor(summary_data$after)

summary_data$mean <- summary_data$mean*100
modelc <- lm("mean~after+ congress + bioguide_id", data=summary_data)
summary(modelc)
confint(modelc)
Effc <- Effect(c("after"), modelc)

predsc <- as.data.frame(Effc)
predsc[,-1] <- round(predsc[,-1],3)
scaleFUN <- function(x) sprintf("%.2f", x)
pc <- ggplot(data=predsc, aes(y=fit, x=after, ymin=lower, ymax=upper)) +
	geom_point() +
	geom_pointrange(aes(ymin=lower, ymax=upper)) +
	xlab("") +
	ylab("Percent of cosponsorship across the aisle")+
	ggtitle("(C) Legislative cosponsorship ")+ 
	coord_flip()+
	theme_pew()+ 
	theme(legend.position = "none") +
	scale_y_continuous(breaks=seq(90,100,by=2), limits = c(90,100),labels=scaleFUN)
pc

#############
# Single plot
#############


cp <- plot_grid(pa, pb, pc, ncol=3)

ggsave(filename="f8.eps", plot=cp,width=12,height=2)
